<template>
  <div class="member-order">
    <XtxTabs v-model="activeName" @tab-click="handleClick">
      <XtxTabsPanel
        v-for="item in orderStatus"
        :key="item.name"
        :name="item.name"
        :label="item.label"
      >
      </XtxTabsPanel>
    </XtxTabs>
    <!-- 订单列表 -->
    <div class="order-list">
      <OrderItem v-for="item in list" :key="item.id" :order="item" />
      <div class="empty" v-if="!list.length">
        空空如也...
      </div>
    </div>
  </div>
</template>

<script>
import { reactive, ref, watch } from 'vue'
import { orderStatus } from '@/constants/order'
import { reqFindOrderList } from '@/api/order'
import OrderItem from './components/order-item.vue'
export default {
  components: { OrderItem },
  name: 'MemberOrder',
  setup () {
    const activeName = ref('all')
    const handleClick = (val) => {
      // console.log('tabs被点击了', val)
      reqParams.orderState = val.index
    }

    // 根据 API 接口发送请求获取订单列表数据
    const reqParams = reactive({
      // 订单状态，0为全部，1为待付款、2为待发货、3为待收货、4为待评价、5为已完成、6为已取消
      orderState: 0,
      page: 1,
      pageSize: 10
    })

    // 列表数据获取
    const list = ref([])

    // 监听请求参数的变化，请求参数有变，从服务器获取最新列表
    watch(reqParams, () => {
      reqFindOrderList(reqParams).then(({ result }) => {
        console.log(result)
        // 🔔 列表的信息在 items 字段中
        list.value = result.items
      })
      // 🔔 immediate: true 立刻执行(默认参数也要发送请求)
    }, { immediate: true })

    return { activeName, handleClick, orderStatus, list }
  }
}
</script>

<style scoped lang="less">
.order-list {
  background: #fff;
  padding: 20px;
}
.empty{
  min-height: 500px;
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>
